package com.hecate.common.result;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.hecate.common.cons.StatusCode;

/**
 * @author 黄星星
 * @date 2020-06-13
 */
public class Result<T> implements IResult<T> {
    private static final long serialVersionUID = 1L;
    private int code;
    private String msg;
    private String details;
    private T data;
    @JsonIgnore
    private Object[] params;

    public Result() {
        this.code = StatusCode.SUCCESS.getCode();
        this.msg = StatusCode.SUCCESS.getDesc();
    }

    public Result(T data) {
        this.code = StatusCode.SUCCESS.getCode();
        this.msg = StatusCode.SUCCESS.getDesc();
        this.data = data;
    }

    public Result(T data, StatusCode statusCode) {
        this.code = StatusCode.SUCCESS.getCode();
        this.msg = StatusCode.SUCCESS.getDesc();
        this.data = data;
        this.code = statusCode.getCode();
        this.msg = statusCode.getDesc();
    }

    public Result(T data, String msg) {
        this.code = StatusCode.SUCCESS.getCode();
        this.msg = StatusCode.SUCCESS.getDesc();
        this.data = data;
        this.msg = msg;
    }

    public Result(StatusCode statusCode) {
        this.code = StatusCode.SUCCESS.getCode();
        this.msg = StatusCode.SUCCESS.getDesc();
        this.code = statusCode.getCode();
        this.msg = statusCode.getDesc();
    }

    private Result(T data, String msg, int code) {
        this.code = StatusCode.SUCCESS.getCode();
        this.msg = StatusCode.SUCCESS.getDesc();
        this.data = data;
        this.msg = msg;
        this.code = code;
    }

    public Result(Throwable e) {
        this.code = StatusCode.SUCCESS.getCode();
        this.msg = StatusCode.SUCCESS.getDesc();
        this.data = this.data;
        this.msg = this.msg;
    }

    @Override
    public int getCode() {
        return this.code;
    }

    @Override
    public String getMsg() {
        return this.msg;
    }

    @Override
    public T getData() {
        return this.data;
    }

    @Override
    public boolean isSuccess() {
        return this.code == 0;
    }

    @Override
    public String getDetails() {
        return this.details;
    }

    @Override
    public void setData(T data) {
        this.data = data;
    }

    @Override
    public void setDetails(String details) {
    }

    @Override
    public void setMsg(String msg) {
        this.msg = msg;
    }

    @Override
    public void setCode(int code) {
        this.code = code;
    }

    @Override
    public Object[] getParams() {
        return this.params;
    }

    @Override
    public void setParams(Object[] params) {
        this.params = params;
    }

    public static <T> Result<T> needLogin(String message, T data) {
        return getJsonResult(data, message);
    }

    public static <T> Result<T> success() {
        return success(null);
    }

    public static <T> Result<T> success(T data) {
        return getJsonResult(data, StatusCode.SUCCESS);
    }

    public static <T> Result<T> error() {
        return getJsonResult(null, (StatusCode)StatusCode.ERROR);
    }

    public static <T> Result<T> error(String message) {
        return new Result((Object)null, message, StatusCode.ERROR.getCode());
    }

    public static <T> Result<T> error(StatusCode statusCode) {
        return getJsonResult(null, (StatusCode)statusCode);
    }

    public static <T> Result<T> error(T data, StatusCode statusCode) {
        return getJsonResult(data, statusCode);
    }

    public static <T> Result<T> error(T data, String message) {
        return new Result(data, message, StatusCode.ERROR.getCode());
    }

    public static <T> Result<T> error(T data, int code, String message) {
        return new Result(data, message, code);
    }

    private static <T> Result<T> getJsonResult(T data, String msg) {
        return new Result(data, msg);
    }

    private static <T> Result<T> getJsonResult(T data, StatusCode statusCode) {
        return new Result(data, statusCode);
    }
}